package Controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Model.*;
import DAO.DAOFactory;
import DAO.EmployeeDAO;
import Controller.*;

/**
 * Servlet implementation class LoginAdmin
 */
@WebServlet("/LoginAdmin")
public class LoginAdmin 
	extends HttpServlet
	implements LoginAuthenticator 
{
	private static final long serialVersionUID = 1L;
	public static final String SESSION_KEY = "UserAdmin";
	public static final String LOGIN_CONTROL = "LoginAdmin";
	   
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginAdmin() {
	    super();
	    // TODO Auto-generated constructor stub
	}
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException
	{
		doProcess(request,response);
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException
	{
		doProcess(request,response);
	}
	
	protected void doProcess(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException
	{
		LoginGeneric.processLogin(request, response,this);
	}

	public static boolean CheckObject(HttpServletRequest request, HttpServletResponse response)
	{
		HttpSession thisSession = request.getSession(false);
		Object tempO = thisSession.getAttribute(SESSION_KEY);
		if (tempO == null) {	//If no object found, redirect back to login
			return false;
		} else {
			return true;
		}
	}
	
	@Override
	//Verify Users Based on Staff
	public Object authenticateUser(String inUserid, String inPassword) {
		EmployeeDAO tempDAO = DAOFactory.getInstance().getEmployeeDAO();
		int userInt = Integer.parseInt(inUserid);
		EmployeeBean emp = null;
		try {
			emp =  tempDAO.findEmployeeById(userInt);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//Check if Employee ID exists
		if (emp != null)		
		{	//Check if Employee is an Admin
			if ( emp.getIsAdmin() )
			{	//Check that Employee is NOT deleted
				if (! emp.getDeleteFlag() )
				{	//Check that Passwords Match
					if ( emp.getPassword().equals(inPassword) )	
					{
						return new UserAdmin( emp.getEmployeeId(), emp.getName() );
					}
				}
			}
		}
		return null;
	}

	@Override
	public String getSessionKey()
	{ return SESSION_KEY; }
	
	@Override
	public String getSuccessPage()
	{ return "EmployeeList"; }
	
	@Override
	public String getCallerControl()
	{ return LOGIN_CONTROL; }	
	
	@Override
	public String getCallerView()
	{ return "LoginAdmin.jsp"; }
}
